using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ProgramMain.OpticNet
{
    public class MapDragObject
    {
        public Point StartPoint { get; private set; }

        public Point EndPoint { get; private set; }

        public int ObjectID { get; private set; }

        public enum DragObjectTypes { None, Vertex, Fibre, MeasurePoint };

        public DragObjectTypes DragObjectType { get; private set; }

        public DragDropEffects DragDropAction { get; private set; }

        private MapDragObject(Point pStartPoint, int pObjectID, DragDropEffects pDragDropEffects, DragObjectTypes pType)
        {
            StartPoint = pStartPoint;
            ObjectID = pObjectID;
            DragObjectType = pType;
            DragDropAction = pDragDropEffects;

        }

        private static void StartDragDrop(Control control, Point StartPoint, int ObjectID, DragDropEffects DragDropEffects, DragObjectTypes DragObjectType)
        {
            control.DoDragDrop(new MapDragObject(StartPoint, ObjectID, DragDropEffects, DragObjectType), DragDropEffects);
        }

        public static void StartNewVertexDragDrop(Control control)
        {
            StartDragDrop(control, Point.Empty, 0, DragDropEffects.Copy, DragObjectTypes.Vertex);
        }

        public static void StartExistingVertexDragDrop(Control control, Point StartPoint, int ObjectID)
        {
            StartDragDrop(control, StartPoint, ObjectID, DragDropEffects.Move, DragObjectTypes.Vertex);
        }

        public static void StartExistingMeasurePointDragDrop(Control control, Point StartPoint, int ObjectID)
        {
            StartDragDrop(control, StartPoint, ObjectID, DragDropEffects.Move, DragObjectTypes.MeasurePoint);
        }

        public static void StartExistingFibreDragDrop(Control control, Point StartPoint, int ObjectID)
        {
            StartDragDrop(control, StartPoint, ObjectID, DragDropEffects.Move, DragObjectTypes.Fibre);
        }

        public static MapDragObject GetObject(Control sender, DragEventArgs dragData)
        {
            MapDragObject dragObject = (MapDragObject)dragData.Data.GetData(typeof(MapDragObject));
            if (dragObject != null && dragObject.DragObjectType == MapDragObject.DragObjectTypes.None)
                dragObject = null;
            
            if (dragObject != null)
            {
                dragObject.EndPoint = sender.PointToClient(new Point(dragData.X, dragData.Y));
            }
            
            return dragObject;
        }

        public static void ChangeCursor(GiveFeedbackEventArgs e, DragObjectTypes DragObjectType)
        {
            if (DragObjectType == DragObjectTypes.Vertex)
            {
                try
                {
                    if (e.Effect == DragDropEffects.Copy
                        || e.Effect == DragDropEffects.Move)
                    {
                        e.UseDefaultCursors = false;

                      //  Stream stream = new MemoryStream(Properties.Resources.cur_vertex_32);
                       // Cursor cursor = new Cursor(stream);
                      //  Cursor.Current = cursor;
                        Cursor.Current = Cursors.Cross;
                    }
                }
                catch (Exception)
                {
                    e.UseDefaultCursors = true;
                }
            }
        }
    }
}